function [nFs, mXY, vShut, bReload] = Random_Intensity(bAutoLoad)
% GalvoScanControl
% Scan script template: Random_Intensity
% Description:
% Run a pulse train at current location with user-defined;
%       number of cycles (#)
%       voltage range (min-max V)
%       number of voltage steps *#(
%       frequency (Hz)
%       duty-cycle (ms)
%

% Scan scripts should return four variables (in this order):
%  nFs      The sample rate (1/sec between scan points)
%  mXY      A matrix with two columns in the format [X Y], where
%               X  are x-positions
%               Y  are y-positions
%           The unit of this matrix is millimeters, relative to the origin
%  vShut    Vector with same length as mXY where values indicate shutter
%           open/close state (when a shutter is controlled) or the beam
%           intensity (when the output is directed to an intensity
%           modulated laser module, e.g. Coherent CUBE).
%           Note: Values of vShut should not exceed +/- 5V. Values beyond
%           these limits will be fixed at +/- 5V by the software.
%  bReload  Boolean indicating whether the script should be re-loaded each
%           time before executing. Setting this to true (1) will decrease
%           the repetition rate and should only be used for long programs
%           that are not executed at high frequency.



% Scan a X-by-Y grid where the beam is exposed for 5 ms
% at each location. Randomize locations.

persistent sCycles sFreq sMinV sMaxV sStepsV sDutyCycle
bReload = 0;

% Default values
if isempty(sCycles), sCycles = '120'; end
if isempty(sDutyCycle), sDutyCycle = '10'; end
if isempty(sFreq), sFreq = '2'; end
if isempty(sMinV), sMinV = '.1'; end
if isempty(sMaxV), sMaxV = '5'; end
if isempty(sStepsV), sStepsV = '10'; end

cAnswers = inputdlg({'Cycles (#)', ...
    'Frequency (Hz)', ...
    'Duty cycle (ms)', ...
    'Minimum intensity (0-5 V)', ...
    'Maximum intensity (0-5 V)', ...
    'Intensity steps (#)'}, ...
    'Random Intensity', ones(1, 6), ...
    {sCycles, sFreq, sDutyCycle, sMinV, sMaxV, sStepsV});
if isempty(cAnswers), return, end

% For some reason, closing the input dialog takes forever.
% Placing this pause here prevents this.
pause(0.1)

sCycles = cAnswers{1};
sFreq = cAnswers{2};
sDutyCycle = cAnswers{3};
sMinV = cAnswers{4};
sMaxV = cAnswers{5};
sStepsV = cAnswers{6};

nCycles = str2double(sCycles);
nFreq = str2double(sFreq);
nDutyCycle = str2double(sDutyCycle);
nMinV = str2double(sMinV);
nMaxV = str2double(sMaxV);
nStepsV = str2double(sStepsV);

% Generate analog trace from current pulse train parameters
nPulseUp = nDutyCycle; % ms
nPeriod = (1 / nFreq) * 1000; % ms
nPulseDown = nPeriod - nPulseUp;

vShutInt = linspace(nMinV, nMaxV, nStepsV);
vShutInt = vShutInt(randperm(length(vShutInt)));

% Truncate intensity to 0 - 5 V
vShutInt(vShutInt < 0) = 0;
vShutInt(vShutInt > 5) = 5;

% Single pulse
vOpen = ones(nPulseUp, 1);      % Open period
vClosed = zeros(nPulseDown, 1); % Close period (0V)

% Verify that we have as many voltage levels as we have pulses.
if length(vShutInt) < nCycles
    vShutInt = repmat(vShutInt, 1, ceil(nCycles/length(vShutInt)));
end

% Compose pulse train
vShut = [];
for i = 1:nCycles
    vShut = [vShut; vOpen .* vShutInt(i); vClosed];
end
vShut = [vShut; 0];
nFs = 1000;
mXY = repmat(0, length(vShut), 2);

return
